﻿using ECBS_Backend.model;
using ECBS_Backend.service;
using ECBS_Backend.dao;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ECBS_Backend.utils;
using Microsoft.AspNetCore.Authorization;

namespace ECBS_Backend.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class SchoolController : ControllerBase
    {
        private readonly AdminService adminService;
        private readonly StudentService studentService;
        private readonly EnrollmentService enrollmentService;
        private readonly GradeService gradeService;

        public SchoolController(AdminService adminService, StudentService studentService, EnrollmentService enrollmentService, GradeService gradeService)
        {
            this.adminService = adminService;
            this.studentService = studentService;
            this.enrollmentService = enrollmentService;
            this.gradeService = gradeService;
        }


        /// <summary>
        /// 学校登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        [HttpGet("login")]
        public ActionResult<string> Login([FromQuery]string userName, [FromQuery]string password)
        {
            try{
                School s = adminService.SchoolLogin(userName, password);
                string token = JwtGenerator.GenerateJWT("School", userName);

                WeIDToken weIDToken = new WeIDToken { WeID = s.WeID, Token = token };

                return Ok(new { message = "登陆成功", data = new { WeID = s.WeID, SchoolName = s.Name, Token = token }, code = 200 });
            }catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        /// <summary>
        /// 添加学生账号
        /// </summary>
        /// <param name="student"></param>
        /// <param name="schoolWeId"></param>
        /// <returns></returns>
        [HttpPost("addStudent")]
        [Authorize(Roles = "School")]
        public ActionResult<Student> AddStudent([FromBody]Student student,[FromQuery]string schoolWeId)
        {
            try{
                Student s = studentService.AddStudent(student, schoolWeId);
                return Ok(new { message = "添加学生成功", data = s, code = 200 });
            }
            catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        
        /// <summary>
        /// 查找学生
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="schoolWeId"></param>
        /// <returns></returns>
        [HttpGet("findStudent")]
        [Authorize(Roles = "School")]
        public ActionResult<List<string>> GetStudent([FromQuery]string keyword,[FromQuery]string schoolWeId)
        {
            try
            {
                List<string> s = studentService.FindStudent(keyword, schoolWeId);
                return Ok(new { message = "获得学生列表成功", data = s, code = 200 });
            }
            catch (Exception e)
            {
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        /// <summary>
        /// 查看学籍是否存在
        /// </summary>
        /// <param name="studentNumber"></param>
        /// <returns></returns>
        [HttpGet("enrollmentExist")]
        [Authorize(Roles = "School")]
        public ActionResult<bool> EnrollmentExist([FromQuery]string studentNumber, [FromQuery]string schoolName)
        {

            bool res = enrollmentService.EnrollmentExist(studentNumber, schoolName);
            return Ok(new { message = "判断学籍是否存在成功", data = res, code = 200 });
            
        }

        /// <summary>
        /// 添加学籍
        /// </summary>
        /// <param name="enrollmentClaim"></param>
        /// <param name="schoolWeID"></param>
        /// <returns></returns>
        [HttpPost("addEnrollment")]
        [Authorize(Roles = "School")]
        public ActionResult<string> AddEnrollment([FromBody]EnrollmentClaim enrollmentClaim, [FromQuery]string schoolWeID)
        {
            try{
                EnrollmentClaim e =  enrollmentService.IssueEnrollment(enrollmentClaim, schoolWeID);
                return Ok(new { message = "增加学籍成功", data = e.EnrollmentClaimId, code = 200 });
            }
            catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        
        /// <summary>
        /// 删除学籍
        /// </summary>
        /// <param name="studentNumber"></param>
        [HttpDelete("deleteEnrollment")]
        [Authorize(Roles = "School")]
        public ActionResult<EnrollmentClaim> DeleteEnrollment([FromQuery]string studentNumber, [FromQuery]string schoolWeId)
        {
            try{
                EnrollmentClaim e = enrollmentService.DeleteEnrollment(studentNumber, schoolWeId);
                return Ok(new { message = "删除学籍成功", data = e, code = 200 });
            }
            catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }

        /// <summary>
        /// 增加成绩
        /// </summary>
        /// <param name="schoolWeID"></param>
        /// <param name="studentNumber"></param>
        /// <param name="gradeDetail"></param>
        /// <returns></returns>
        [HttpPost("addGrade")]
        [Authorize(Roles = "School")]
        public ActionResult<GradeClaim> AddGrade([FromQuery]string schoolWeID, [FromQuery] string studentNumber, [FromBody]GradeDetail gradeDetail)
        {
            try{
                GradeClaim g =  gradeService.IssueGrade(schoolWeID, studentNumber, gradeDetail);
                return Ok(new { message = "增加成绩成功", data = g, code = 200 });
            }
            catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }


        /// <summary>
        /// 删除某学生的某条成绩
        /// </summary>
        /// <param name="studentNumber"></param>
        /// <param name="gradeName"></param>
        /// <param name="schoolWeID"></param>
        /// <returns></returns>
        [HttpDelete("deleteGrade")]
        [Authorize(Roles = "School")]
        public ActionResult<GradeClaim> DeleteGrade([FromQuery]string studentNumber, [FromQuery]string gradeName, [FromQuery]string schoolWeID)
        {
            try{
                GradeClaim g = gradeService.DeleteGrade(studentNumber, gradeName, schoolWeID);
                return Ok(new { message = "删除成绩成功", data = g, code = 200 });
            }
            catch(Exception e){
                return BadRequest(new { message = e.Message, code = 500 });
            }
        }
    }
}
